# -*-coding = utf-8 -*-

# @Time: 2021/4/2 19:14
# @Author: bistro
# @File: p17.21.py
# @Software: PyCharm
from typing import List


class Solution:
    def trap(self, height: List[int]) -> int:
        area = 0  # 最大水量
        if height:
            length = len(height)  # 长度
            larea = rarea = 0  # 从两边扫描面积
            for i in range(length):
                larea = max(larea, height[i])
                rarea = max(rarea, height[length - i - 1])
                area += larea
                area += rarea
                area -= height[i]
            area -= max(height) * length  # 最大面积 = S左 + S右 - 整个 - S柱子
        return area


"""
测试用例
s = Solution()
res = s.trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])
print(res)
"""
